package com.lishe.lishe.lishe.module.me.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.me.bean.AddressItem;
import com.lishe.lishe.lishe.module.me.others.AddressViewHolder;

import java.util.LinkedList;

/**
 * Created by zhcasecom on 2017/8/22.
 */

public class AddressAdapter extends BaseAdapter implements View.OnClickListener {
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private LinkedList<AddressItem> list = new LinkedList<AddressItem>();
    public AddressViewHolder holder = null;
    private Callback mCallback;

    public AddressAdapter(Context context, LinkedList<AddressItem> list, Callback callback) {
        this.context = context;
        this.list.addAll(list);
        this.mCallback = callback;

    }

    public void resetData(LinkedList<AddressItem> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    public interface Callback {
        public void click(View v);
    }

    @Override
    public void onClick(View view) {
        mCallback.click(view);
    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            view = View.inflate(context, R.layout.address_item, null);
            holder = new AddressViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (AddressViewHolder) view.getTag();
        }

        AddressItem addressItem = list.get(i);
        holder.mobile.setText(addressItem.getMobile());
        holder.name.setText(addressItem.getName());
        String areaStr = addressItem.getArea().replace("/", "");
        String addrStr = addressItem.getAddr().replace("/", "");
        holder.addr_detail.setText(areaStr + addrStr);
        if (addressItem.getDef_addr() == 1) {
            holder.setDefaultBtn.setBackgroundResource(R.mipmap.sel_choice_icon);
        } else {
            holder.setDefaultBtn.setBackgroundResource(R.mipmap.nor_choice_icon);
        }
//        holder.editBtn.setIndex(i);
//        holder.editBtn.setAddrId(addressItem.addr_id);
//        holder.setDefaultBtn.setAddrId(addressItem.addr_id);
//        holder.setDefaultBtn.setIndex(i);
        holder.editBtn.setTag(i);
        holder.setDefaultBtn.setTag(i);
        holder.editBtn.setOnClickListener(this);
        holder.setDefaultBtn.setOnClickListener(this);
//        Log.e(TAG, "第" + i + "个——————————————————");
        return view;
    }
}
